package org.road0329.dlt698.axdr;

import org.road0329.dlt698.ReverseByteArrayInputStream;
import org.road0329.dlt698.ReverseByteArrayOutputStream;
import org.road0329.dlt698.asn1.axdr.AxdrEnum;
import org.road0329.dlt698.asn1.axdr.AxdrNull;
import org.road0329.dlt698.asn1.axdr.AxdrType;

import java.io.IOException;


public class ConnectMechanismInfo extends AxdrType {
    private byte[] dataCode = null;
    private Choice choice = Choice._ERR_NONE_SELECTED;
    private AxdrNull nullSecurity = null;
    private PasswordSecurity passwordSecurity = null;
    private SymmetrySecurity symmetrySecurity = null;
    private SignatureSecurity signatureSecurity = null;

    public ConnectMechanismInfo() {
    }

    public void set_dataCode(byte[] dataCode) {
        this.dataCode = dataCode;
    }

    public void setNullSecurity(AxdrNull nullSecurity) {
        resetChoices();
        this.choice = Choice.NULL_SECURITY;
        this.nullSecurity = nullSecurity;
    }

    public void setPasswordSecurity(PasswordSecurity passwordSecurity) {
        resetChoices();
        this.choice = Choice.PASSWORD_SECURITY;
        this.passwordSecurity = passwordSecurity;
    }

    public void setSymmetrySecurity(SymmetrySecurity symmetrySecurity) {
        resetChoices();
        this.choice = Choice.SYMMETRY_SECURITY;
        this.symmetrySecurity = symmetrySecurity;
    }

    public void setSignatureSecurity(SignatureSecurity signatureSecurity) {
        resetChoices();
        this.choice = Choice.SIGNATURE_SECURITY;
        this.signatureSecurity = signatureSecurity;
    }

    public int decode(ReverseByteArrayInputStream input) throws Exception {
        int codeLength = 0;
        AxdrEnum choosen = new AxdrEnum();
        choosen.setConst();
        codeLength += choosen.decode(input);
        resetChoices();
        this.choice = Choice.getEnumByValue((int)choosen.getValue());
        if (this.choice == Choice.NULL_SECURITY) {
            this.nullSecurity = new AxdrNull();
            codeLength += this.nullSecurity.decode(input);
            return codeLength;
        } else if (this.choice == Choice.PASSWORD_SECURITY) {
            this.passwordSecurity = new PasswordSecurity();
            codeLength += this.passwordSecurity.decode(input);
            return codeLength;
        } else if (this.choice == Choice.SYMMETRY_SECURITY) {
            this.symmetrySecurity = new SymmetrySecurity();
            codeLength += this.symmetrySecurity.decode(input);
            return codeLength;
        } else if (this.choice == Choice.SIGNATURE_SECURITY) {
            this.signatureSecurity = new SignatureSecurity();
            codeLength += this.signatureSecurity.decode(input);
            return codeLength;
        }
        throw new Exception("Error decoding AxdrChoice: Identifier matched to no item.");
    }

    public int encode(ReverseByteArrayOutputStream output) throws Exception {
        if (this.dataCode != null) {
            for (int i = this.dataCode.length - 1; i >= 0; i--) {
                output.write(this.dataCode[i]);
            }
            return this.dataCode.length;
        }
        if (this.choice == Choice._ERR_NONE_SELECTED) {
            throw new Exception("Error encoding AxdrChoice: No item in choice was selected.");
        }
        int codeLength = 0;
        if (this.choice == Choice.SIGNATURE_SECURITY) {
            if (this.signatureSecurity != null) {
                codeLength += this.signatureSecurity.encode(output);
                AxdrEnum c = new AxdrEnum();
                c.setMinMaxVal(3);
                codeLength += c.encode(output);
                return codeLength;
            } else {
                throw new Exception("signatureSecurity is null");
            }
        } else if (this.choice == Choice.SYMMETRY_SECURITY) {
            if (this.symmetrySecurity != null) {
                codeLength += this.symmetrySecurity.encode(output);
                AxdrEnum c = new AxdrEnum();
                c.setMinMaxVal(2);
                codeLength += c.encode(output);
                return codeLength;
            } else {
                throw new Exception("symmetrySecurity is null");
            }
        } else if (this.choice == Choice.PASSWORD_SECURITY) {
            if (this.passwordSecurity != null) {
                codeLength += this.passwordSecurity.encode(output);
                AxdrEnum c = new AxdrEnum();
                c.setMinMaxVal(1);
                codeLength += c.encode(output);
                return codeLength;
            } else {
                throw new Exception("passwordSecurity is null");
            }
        } else if (this.choice == Choice.NULL_SECURITY) {
            if (this.nullSecurity != null) {
                codeLength += this.nullSecurity.encode(output);
                AxdrEnum c = new AxdrEnum();
                c.setMinMaxVal(0);
                codeLength += c.encode(output);
                return codeLength;
            } else {
                throw new Exception("nullSecurity is null");
            }
        }
        throw new Exception("Error encoding AxdrChoice: No item in choice was encoded.");
    }

    public Choice getChoiceIndex() {
        return this.choice;
    }

    public void resetChoices() {
        this.choice = Choice._ERR_NONE_SELECTED;
        this.nullSecurity = null;
        this.passwordSecurity = null;
        this.symmetrySecurity = null;
        this.signatureSecurity = null;
    }

    public void encodeAndSave(int encodingSizeGuess) throws Exception {
        ReverseByteArrayOutputStream revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        encode(revOStream);
        this.dataCode = revOStream.getArray();
    }

    public String toString() {
        if (this.choice == Choice.NULL_SECURITY) {
            if (this.nullSecurity != null) {
                return "choice: {nullSecurity: " + this.nullSecurity + "}";
            } else {
                return "choice is nullSecurity but nullSecurity is null";
            }
        } else if (this.choice == Choice.PASSWORD_SECURITY) {
            if (this.passwordSecurity != null) {
                return "choice: {passwordSecurity: " + this.passwordSecurity + "}";
            } else {
                return "choice is passwordSecurity but passwordSecurity is null";
            }
        } else if (this.choice == Choice.SYMMETRY_SECURITY) {
            if (this.symmetrySecurity != null) {
                return "choice: {symmetrySecurity: " + this.symmetrySecurity + "}";
            } else {
                return "choice is symmetrySecurity but symmetrySecurity is null";
            }
        } else if (this.choice == Choice.SIGNATURE_SECURITY) {
            if (this.signatureSecurity != null) {
                return "choice: {signatureSecurity: " + this.signatureSecurity + "}";
            } else {
                return "choice is signatureSecurity but signatureSecurity is null";
            }
        } else {
            return "unknown";
        }
    }

    enum Choice {
        _ERR_NONE_SELECTED(-1),
        NULL_SECURITY(0),
        PASSWORD_SECURITY(1),
        SYMMETRY_SECURITY(2),
        SIGNATURE_SECURITY(3);

        private final int value;

        Choice(int value) {
            this.value = value;
        }

        public int getValue() {
            return value;
        }

        public static Choice getEnumByValue(int value) {
            for (Choice item : Choice.values()) {
                if (value == item.getValue()) {
                    return item;
                }
            }
            return null;
        }
    }
}



